package com.software.software.model.server;

import com.software.software.model.userInfo;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.util.Date;
import java.util.Random;

@Component
public class EmailUtil {

    @Resource
    private JavaMailSender javaMailSender;

    public String sendMail(String mail, userInfo user) {
        String row = null;
        MimeMessage msg = javaMailSender.createMimeMessage();
        try {
            Random r = new Random();
                int ran1 = r.nextInt(899999)+100000;
                String text=(user!=null) ? (("尊敬的用户："+user.loginName) +"，"):"";
                text=text+"您本次的验证码为："+ran1;
                System.out.println(ran1);
            MimeMessageHelper helper =
                    new MimeMessageHelper(msg, true);
            helper.setText(text, true);
            helper.addTo(mail);
            helper.setFrom("Super_O2@163.com");
            helper.setSubject("验证码");
            helper.setSentDate(new Date());
            javaMailSender.send(msg);
            row = String.valueOf(ran1);
        } catch (MessagingException e) {
            e.printStackTrace();
        }
        return row;
    }
}
